package cn.maka.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;

        String requestURI = request.getRequestURI();

        if (requestURI.contains("/login.jsp") || requestURI.contains("/loginServlet")
                || requestURI.contains("/checkServlet") || requestURI.contains("/css/")
                || requestURI.contains("/js/") || requestURI.contains("/fonts/") || requestURI.contains("/img/") ){

            chain.doFilter(req, resp);

        }else {
            HttpSession session = request.getSession();
            Object user = session.getAttribute("user");

            if (user!=null){
                chain.doFilter(req, resp);
            }else {
                request.setAttribute("error","用户未登录");
                request.getRequestDispatcher("/login.jsp").forward(request,resp);
            }
        }


    }

    public void init(FilterConfig config) throws ServletException {

    }

}
